
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Physical Memory Protection &#8212; Freedom Metal v201905 documentation</title>
    <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Standard I/O" href="tty.html" />
    <link rel="prev" title="Instruction Tightly Integrated Memory" href="itim.html" />
   
  <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <div class="section" id="physical-memory-protection">
<h1>Physical Memory Protection<a class="headerlink" href="#physical-memory-protection" title="Permalink to this headline">¶</a></h1>
<p>Physical Memory Protection (PMP) is a part of the RISC-V Privileged Architecture
Specification which discribes the interface for a standard RISC-V memory
protection unit.</p>
<p>The PMP defines a finite number of PMP regions which can be individually configured
to enforce access permissions to a range of addresses in memory. Each PMP region
is configurable with the following options:</p>
<dl class="class">
<dt>
<em class="property">struct </em><code class="descname">metal_pmp_config</code><br /></dt>
<dd><p>Configuration for a PMP region. </p>
<div class="breathe-sectiondef docutils container">
<p class="breathe-sectiondef-title rubric">Public Types</p>
<dl class="type">
<dt>
<em class="property">enum </em><code class="descname">metal_pmp_locked</code><br /></dt>
<dd><p>Sets whether the PMP region is locked. </p>
<p><em>Values:</em></p>
<dl class="enumerator">
<dt>
<code class="descname">METAL_PMP_UNLOCKED</code> = 0<br /></dt>
<dd></dd></dl>

<dl class="enumerator">
<dt>
<code class="descname">METAL_PMP_LOCKED</code> = 1<br /></dt>
<dd></dd></dl>

</dd></dl>

</div>
<div class="breathe-sectiondef docutils container">
<p class="breathe-sectiondef-title rubric">Public Members</p>
<dl class="member">
<dt>
int <code class="descname">R</code><br /></dt>
<dd><p>Sets whether reads to the PMP region succeed. </p>
</dd></dl>

<dl class="member">
<dt>
int <code class="descname">W</code><br /></dt>
<dd><p>Sets whether writes to the PMP region succeed. </p>
</dd></dl>

<dl class="member">
<dt>
int <code class="descname">X</code><br /></dt>
<dd><p>Sets whether the PMP region is executable. </p>
</dd></dl>

<dl class="member">
<dt>
<a class="reference internal" href="../apiref/pmp.html#_CPPv322metal_pmp_address_mode" title="metal_pmp_address_mode">metal_pmp_address_mode</a> <code class="descname">A</code><br /></dt>
<dd><p>Sets the addressing mode of the PMP region. </p>
</dd></dl>

</div>
</dd></dl>

<div class="section" id="initializing-the-pmp">
<h2>Initializing the PMP<a class="headerlink" href="#initializing-the-pmp" title="Permalink to this headline">¶</a></h2>
<p>All PMP-related functions first depend on having a handle to the PMP device:</p>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">metal_pmp</span> <span class="o">*</span><span class="n">pmp</span> <span class="o">=</span> <span class="n">metal_pmp_get_device</span><span class="p">();</span>
<span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">pmp</span><span class="p">)</span> <span class="p">{</span>
   <span class="cm">/* Failed to get PMP device handle */</span>
<span class="p">}</span>
</pre></div>
</div>
<p>PMP initialization is optional and has the effect of disabling all PMP regions,
if possible:</p>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="n">metal_pmp_init</span><span class="p">(</span><span class="n">pmp</span><span class="p">);</span>
</pre></div>
</div>
<p>The number of PMP regions available can be retrieved from the PMP device handle:</p>
<dl class="class">
<dt>
<em class="property">struct </em><code class="descname">metal_pmp</code><br /></dt>
<dd><p>A handle for the PMP device. </p>
</dd></dl>

</div>
<div class="section" id="configuring-a-pmp-region">
<h2>Configuring a PMP Region<a class="headerlink" href="#configuring-a-pmp-region" title="Permalink to this headline">¶</a></h2>
<p>Freedom Metal has a set of APIs for configuring a PMP region. The most generic of these
is</p>
<dl class="function">
<dt id="_CPPv320metal_pmp_set_regionP9metal_pmpj16metal_pmp_config6size_t">
<span id="_CPPv220metal_pmp_set_regionP9metal_pmpj16metal_pmp_config6size_t"></span><span id="metal_pmp_set_region__metal_pmpP.unsigned-i.metal_pmp_config.s"></span><span class="target" id="pmp_8h_1acdbdbc7cc2727d4ae10205617aee3013"></span>int <code class="descname">metal_pmp_set_region</code><span class="sig-paren">(</span><em class="property">struct</em> <a class="reference internal" href="../apiref/pmp.html#_CPPv39metal_pmp" title="metal_pmp">metal_pmp</a> *<em>pmp</em>, unsigned int <em>region</em>, <em class="property">struct</em> <a class="reference internal" href="../apiref/pmp.html#_CPPv316metal_pmp_config" title="metal_pmp_config">metal_pmp_config</a> <em>config</em>, size_t <em>address</em><span class="sig-paren">)</span><br /></dt>
<dd><p>Configure a PMP region. </p>
<p><dl class="docutils">
<dt><strong>Return</strong></dt>
<dd>0 upon success </dd>
<dt><strong>Parameters</strong></dt>
<dd><ul class="breatheparameterlist first last simple">
<li><code class="docutils literal notranslate"><span class="pre">pmp</span></code>: The PMP device handle </li>
<li><code class="docutils literal notranslate"><span class="pre">region</span></code>: The PMP region to configure </li>
<li><code class="docutils literal notranslate"><span class="pre">config</span></code>: The desired configuration of the PMP region </li>
<li><code class="docutils literal notranslate"><span class="pre">address</span></code>: The desired address of the PMP region </li>
</ul>
</dd>
</dl>
</p>
</dd></dl>

<p>This function allows for the configuration of all PMP region settings.</p>
<p>Additional APIs are provided for granularly changing individual PMP region settings.
For example:</p>
<dl class="function">
<dt>
int <code class="descname">metal_pmp_set_address</code><span class="sig-paren">(</span><em class="property">struct</em> <a class="reference internal" href="../apiref/pmp.html#_CPPv39metal_pmp" title="metal_pmp">metal_pmp</a> *<em>pmp</em>, unsigned int <em>region</em>, size_t <em>address</em><span class="sig-paren">)</span><br /></dt>
<dd><p>Set the address for a PMP region. </p>
<p><dl class="docutils">
<dt><strong>Return</strong></dt>
<dd>0 if the address is successfully set </dd>
<dt><strong>Parameters</strong></dt>
<dd><ul class="breatheparameterlist first last simple">
<li><code class="docutils literal notranslate"><span class="pre">pmp</span></code>: The PMP device handle </li>
<li><code class="docutils literal notranslate"><span class="pre">region</span></code>: The PMP region to set </li>
<li><code class="docutils literal notranslate"><span class="pre">address</span></code>: The desired address of the PMP region </li>
</ul>
</dd>
</dl>
</p>
</dd></dl>

<dl class="function">
<dt>
int <code class="descname">metal_pmp_lock</code><span class="sig-paren">(</span><em class="property">struct</em> <a class="reference internal" href="../apiref/pmp.html#_CPPv39metal_pmp" title="metal_pmp">metal_pmp</a> *<em>pmp</em>, unsigned int <em>region</em><span class="sig-paren">)</span><br /></dt>
<dd><p>Lock a PMP region. </p>
<p><dl class="docutils">
<dt><strong>Return</strong></dt>
<dd>0 if the region is successfully locked </dd>
<dt><strong>Parameters</strong></dt>
<dd><ul class="breatheparameterlist first last simple">
<li><code class="docutils literal notranslate"><span class="pre">pmp</span></code>: The PMP device handle </li>
<li><code class="docutils literal notranslate"><span class="pre">region</span></code>: The PMP region to lock </li>
</ul>
</dd>
</dl>
</p>
</dd></dl>

<dl class="function">
<dt>
int <code class="descname">metal_pmp_set_writeable</code><span class="sig-paren">(</span><em class="property">struct</em> <a class="reference internal" href="../apiref/pmp.html#_CPPv39metal_pmp" title="metal_pmp">metal_pmp</a> *<em>pmp</em>, unsigned int <em>region</em>, int <em>W</em><span class="sig-paren">)</span><br /></dt>
<dd><p>Set the writable bit for a PMP region. </p>
<p><dl class="docutils">
<dt><strong>Return</strong></dt>
<dd>0 if the writable bit is successfully set </dd>
<dt><strong>Parameters</strong></dt>
<dd><ul class="breatheparameterlist first last simple">
<li><code class="docutils literal notranslate"><span class="pre">pmp</span></code>: The PMP device handle </li>
<li><code class="docutils literal notranslate"><span class="pre">region</span></code>: The PMP region to set </li>
<li><code class="docutils literal notranslate"><span class="pre">W</span></code>: The desired value of the writable bit </li>
</ul>
</dd>
</dl>
</p>
</dd></dl>

<p>Additional documentation for this API is provided in <a class="reference internal" href="../apiref/pmp.html"><span class="doc">the PMP API reference</span></a>.</p>
<p>The RISC-V specification allows implementation of PMP to hard-wire the configuration
values of PMP regions. In these cases, attempts to configure these PMP regions will
fail.</p>
</div>
<div class="section" id="handling-pmp-access-faults">
<h2>Handling PMP Access Faults<a class="headerlink" href="#handling-pmp-access-faults" title="Permalink to this headline">¶</a></h2>
<p>Attempted memory accesses which the PMP is configured to prevent trigger a
CPU exception. These exceptions can be handled by installing a CPU exception
handler for exception codes related to memory access faults.</p>
<p>Additional documentation about creating and registering exception handlers can
be found in <a class="reference internal" href="exceptions.html"><span class="doc">the Exception Handlers Developer Guide</span></a>.</p>
</div>
<div class="section" id="additional-documentation">
<h2>Additional Documentation<a class="headerlink" href="#additional-documentation" title="Permalink to this headline">¶</a></h2>
<p>Additional documentation about the Physical Memory Protection system and fault
handling on RISC-V systems can be found in
<a class="reference external" href="https://riscv.org/specifications/privileged-isa/">The RISC-V Privileged ISA Specification v1.10</a>.</p>
</div>
</div>


          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../index.html">Freedom Metal</a></h1>








<h3>Navigation</h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../introduction.html">Introduction to Freedom Metal</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../devguide.html">Developer Guide</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="exceptions.html">Exception Handlers</a></li>
<li class="toctree-l2"><a class="reference internal" href="fe310-g000-pll.html">FE310-G00 PLL</a></li>
<li class="toctree-l2"><a class="reference internal" href="interrupts.html">Interrupt Handlers</a></li>
<li class="toctree-l2"><a class="reference internal" href="itim.html">Instruction Tightly Integrated Memory</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Physical Memory Protection</a></li>
<li class="toctree-l2"><a class="reference internal" href="tty.html">Standard I/O</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../api.html">API Reference</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../index.html">Documentation overview</a><ul>
  <li><a href="../devguide.html">Developer Guide</a><ul>
      <li>Previous: <a href="itim.html" title="previous chapter">Instruction Tightly Integrated Memory</a></li>
      <li>Next: <a href="tty.html" title="next chapter">Standard I/O</a></li>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2019, SiFive Inc..
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.7.5</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.11</a>
      
      |
      <a href="../_sources/devguide/pmps.rst.txt"
          rel="nofollow">Page source</a>
    </div>

    

    
  </body>
</html>